package com.guigu.vod.service.impl;

import com.aliyun.vod.upload.impl.UploadVideoImpl;
import com.aliyun.vod.upload.req.UploadStreamRequest;
import com.aliyun.vod.upload.resp.UploadStreamResponse;
import com.aliyuncs.DefaultAcsClient;
import com.aliyuncs.profile.DefaultProfile;
import com.aliyuncs.vod.model.v20170321.DeleteVideoRequest;
import com.aliyuncs.vod.model.v20170321.GetPlayInfoRequest;
import com.aliyuncs.vod.model.v20170321.GetPlayInfoResponse;
import com.guigu.vod.service.VodService;
import com.guigu.vod.utils.ConstantVodUtils;
import com.guigu.vod.utils.InitVodCilent;
import org.apache.commons.lang.StringUtils;
import org.springframework.stereotype.Service;
import org.springframework.web.multipart.MultipartFile;

import java.io.IOException;
import java.io.InputStream;
import java.util.List;

@Service
public class VodServiceImpl implements VodService {
    @Override
    public String uploadVideoAly(MultipartFile file) {
        try {
            //fileName
            // 上传文件原始名称    test-aa.mp4
            String fileName = file.getOriginalFilename();
            //title 上传之后显示名称，就是原始文件名(可以根据实际需要，改文件名)
            String title = fileName.substring(0, fileName.lastIndexOf("."));

            //inputStream 上传文件输入流
            InputStream inputStream = file.getInputStream();
            // 创建上传视频的请求
            UploadStreamRequest request = new UploadStreamRequest(ConstantVodUtils.ACCESS_KEY_ID,
                    ConstantVodUtils.ACCESS_KEY_SECRET,
                    title,
                    fileName,
                    inputStream);

            // 视频的存储地址
            request.setStorageLocation("outin-753f0bd96c1e11f09d1f00163e10d579.oss-cn-shanghai.aliyuncs.com");
            // 视频的存储服务器区域名称
            request.setApiRegionId("cn-shanghai");
            // 创建上传视频的实现类
            UploadVideoImpl uploader = new UploadVideoImpl();
            // 真正的上传视频
            UploadStreamResponse response = uploader.uploadStream(request);
            String videoId = null;
            if (response.isSuccess()) {
                // 响应成功，获取到上传的视频id
                videoId = response.getVideoId();
            } else {
                //如果设置回调URL无效，不影响视频上传，可以返回VideoId同时会返回错误码。其他情况上传失败时，VideoId为空，此时需要根据返回错误码分析具体错误原因
                videoId = response.getVideoId();
            }
            return videoId;
        } catch (IOException e) {
            e.printStackTrace();
        }
        return null;
    }

    @Override
    public void deleteById(List<String> videoIdList) {
        try {
            //初始化操作视频点播相关服务的客户端对象
            DefaultAcsClient client = InitVodCilent.initVodClient(ConstantVodUtils.ACCESS_KEY_ID, ConstantVodUtils.ACCESS_KEY_SECRET);
            // 删除视频的请求对象
            DeleteVideoRequest request = new DeleteVideoRequest();
            // 转换要删除的id集合为字符串
            String videoIds = StringUtils.join(videoIdList.toArray(), ",");
            request.setVideoIds(videoIds);
            client.getAcsResponse(request);
        } catch (Exception e) {
            e.printStackTrace();
            throw new RuntimeException("删除视频失败");
        }
    }
    @Override
    public String getPlayUrl(String videoId) {
        // 初始化VOD客户端
        DefaultAcsClient client =InitVodCilent.initVodClient(ConstantVodUtils.ACCESS_KEY_ID, ConstantVodUtils.ACCESS_KEY_SECRET);
        //创建获取视频地址
        GetPlayInfoRequest request = new GetPlayInfoRequest();
        request.setVideoId(videoId);
        try {
            // 获取视频播放地址
            return client.getAcsResponse(request).getPlayInfoList().get(0).getPlayURL();
        } catch (Exception e) {
            throw new RuntimeException("获取播放地址失败", e);
        }
    }


//    public static void main(String[] args) {
//        List<String> list  = new ArrayList<>();
//        list.add("1");
//        list.add("2");
//        list.add("3");
//
//        String join = StringUtils.join(list.toArray(), ",");
//        System.out.println(join);
//    }
}
